package com.compomics.proteocloud.jobs;

import java.awt.EventQueue;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.compomics.proteocloud.sqs.MessageManager;
import com.compomics.proteocloud.util.AWSUtils;
import com.xerox.amazonws.sqs2.Message;
import com.xerox.amazonws.sqs2.SQSException;

/**
 * This class aims to listen to the status queue, receive the messages and convert them to job objects. 
 * @author Thilo Muth
 *
 */
public class StatusQueueListener {
	
	/**
	 *  Property change support for notifying the user interface about new messages.
	 */
	private PropertyChangeSupport pSupport;
	
	/**
	 * Message manager instance.
	 */
	private MessageManager msgManager;
	
	/**
	 * Singleton fo the StatusQueueListener
	 */
	private static StatusQueueListener statusQueueListener;
	
	/**
	 * Constructor of the StatusQueueListener.
	 * @param msgManager	 
	 */
	private StatusQueueListener(MessageManager msgManager) {
		this.msgManager = msgManager;
		pSupport = new PropertyChangeSupport(this);
	}
	
	/**
	 * Retrieve the instance (singleton object) of this class.
	 * @return statusQueueListener StatusQueueListener instance
	 */
	public static StatusQueueListener getInstance() {
		if (statusQueueListener == null) {
            MessageManager msgManager = null;
            try {
                msgManager = new MessageManager(AWSUtils.getProperties(), "statusQueue");
                statusQueueListener = new StatusQueueListener(msgManager);
            } catch (SQSException e) {
                e.printStackTrace();
            }
		}
		return statusQueueListener;
	}
	
	/**
	 * Start the listener.
	 */
	public void start() {
		Message message;
		try {
			while(true){
				message = msgManager.receiveMessage();

				// Check if message was received.
				if (message != null) {
					final CloudJob cloudJob = new CloudJob();
					StringTokenizer tokenizer = new StringTokenizer(message.getMessageBody());
					List<String> tokenList = new ArrayList<String>();

					// Iterate over all the tokens
					while (tokenizer.hasMoreTokens()) {
						tokenList.add(tokenizer.nextToken());
					}
					cloudJob.setInstanceID(tokenList.get(0));
					cloudJob.setCurrentJob(tokenList.get(1));

                    // Check for the mgf filename
                    if(tokenList.size() > 2) {
                        cloudJob.setCurrentFile(tokenList.get(2));
                    }
					
					// Check for error messages
					if(tokenList.size() > 3) {					
						cloudJob.setError(tokenList.get(3));
					}
					
					// Fire property change (New Jobstatus)
					EventQueue.invokeLater(new Runnable() {						
						public void run() {
							pSupport.firePropertyChange("New Jobstatus", null, cloudJob);
						}
					});

					// Finally delete the message
					msgManager.deleteMessage(message);
				}
			}
		} catch (SQSException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Adds the property change listener.
	 * @param l
	 */
	public void addPropertyChangeListener(PropertyChangeListener l ) { 
    	pSupport.addPropertyChangeListener(l); 
	} 
}
